package com.abhimanyu.bootstrap.processors;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;

import com.abhimanyu.bootstrap.enums.BootstrapConstants;
import com.abhimanyu.bootstrap.util.JobContext;
import com.abhimanyu.dataaccess.model.User;
import com.abhimanyu.messaging.model.IMessage;
import com.abhimanyu.messaging.model.TextMessage;
import com.abhimanyu.service.model.DollarRateServiceData;

@Component
public class MessageTransformProcessor implements ItemProcessor<User, IMessage> {
	Logger logger = Logger.getLogger(getClass());

	@SuppressWarnings("unchecked")
	public IMessage process(User user) throws Exception {
	
		List<DollarRateServiceData> dollarRates = (List<DollarRateServiceData>) JobContext
				.getInstance().get(BootstrapConstants.DOLLAR_RATES);
		logger.debug(dollarRates);
		String dollarRateMessage = constructMessageText(dollarRates);
		IMessage message = transformToMessage(user, dollarRateMessage);
		return message;
	}

	
	private IMessage transformToMessage(User user, String messageText) {
		List<IMessage> messagesList = new ArrayList<IMessage>();
		TextMessage message = null;

		message = new TextMessage();
		message.setScheduledAt(user.getScheduledAt());
		message.setPhoneNumber(user.getMobileNumber());
		message.setMessageText(messageText);
		messagesList.add(message);

		return message;
	}

	private String constructMessageText(List<DollarRateServiceData> serviceList) {
		StringBuffer sb = new StringBuffer();
		String messageText = null;

		if (serviceList.size() > 1) {
			// Identify best rate.
			findBestRate(serviceList);
			sb.append("Best Dollar rate today is : ").append(
					serviceList.get(0).getServiceName() + " : "
							+ serviceList.get(0).getDollarRateData());
			sb.append("\n").append("Exchange Providers");
		} else {
			sb.append("Dollar rate today is");
		}
		List<String> rates = constructMessageString(serviceList);
		for (String rate : rates) {
			sb.append("\n- ").append(rate);
		}

		//sb.append("\n- Service by Abhimanyu's Daily Dollar Rate Notifier.");
		messageText = sb.toString();
		System.out.println("TEXT CONSTRUCTED IS : " + sb.toString());
		return messageText;
	}

	private void findBestRate(List<DollarRateServiceData> serviceList) {
		Collections.sort(serviceList, new Comparator<DollarRateServiceData>() {
			public int compare(DollarRateServiceData o1, DollarRateServiceData o2) {
				return Double.valueOf(o2.getDollarRateData()).compareTo(
						Double.valueOf(o1.getDollarRateData()));

			}
		});
		serviceList.add(serviceList.remove(0));
		System.out.println("SORTED LIST : " + serviceList);
	}

	private List<String> constructMessageString(
			List<DollarRateServiceData> serviceList) {
		System.out.println("SEEEEEEEEEEEEEEEEEEEEEEEE : " + serviceList);
		List<String> dollarRates = new ArrayList<String>();
		for (DollarRateServiceData dollarRateData : serviceList) {
			dollarRates.add(dollarRateData.getServiceName() + " : "
					+ dollarRateData.getDollarRateData());
		}
		return dollarRates;
	}
}
